﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HFController
{
    /// <summary>
    /// 律师业务逻辑
    /// </summary>
    public class LawyerController:ControllerBase
    {
        //单列
        private static LawyerController _lawyerController;
        //实例化单列
        public static LawyerController getController()
        {
            if (_lawyerController == null)
            {
                _lawyerController = new LawyerController();
            }
            return _lawyerController;
        }

        /// <summary>
        /// 律师数据库访问
        /// </summary>
        private hf.DAL.Lawyer LawyerDAL = new hf.DAL.Lawyer();
        private hf.DAL.LawyerNote noteDAL = new hf.DAL.LawyerNote();
        private hf.DAL.DelegateLawyer delegateDAL = new hf.DAL.DelegateLawyer();

        /// <summary>
        /// 得到接洽律师的ID
        /// </summary>
        /// <param name="DelegateID"></param>
        /// <returns></returns>
        public string getDelegateLawyerIDS(int DelegateID)
        {
            return delegateDAL.GetIDList(DelegateID);
        }
        /// <summary>
        /// 得到接洽答案的ID
        /// </summary>
        /// <param name="LawyerID"></param>
        /// <returns></returns>
        public string getDelegeteIDS(int LawyerID)
        {
            return delegateDAL.GetDelegateIDList(LawyerID);
        }

       /// <summary>
       /// 发布接洽消息
       /// </summary>
       /// <param name="DelegateID"></param>
       /// <returns></returns>
        public HFModel.ResultModel doContectDelegate(int DelegateID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            BagePages bp = new BagePages();
            hf.Model.Lawyer lawyer = bp.getLoginLawyer();
            if (lawyer == null)
            {
                result.isTrue = false;
                result.Message = "请先登录！";
            }
            else {
                int total = 0;
                List<hf.Model.DelegateLawyer> list = delegateDAL.GetDelegateLawyerList(1, 1, "LawyerID=" + lawyer.ID.ToString() + " and DelegateID=" + DelegateID.ToString(), out total);
                if (list.Count > 0)
                {
                    result.isTrue = false;
                    result.Message = "您已经对此委托进行接洽，请勿重复接洽！";
                }
                else {
                    hf.Model.DelegateLawyer dl = new hf.Model.DelegateLawyer();
                    dl.DelegateID = DelegateID;
                    dl.LawyerID = lawyer.ID;
                    try {
                        delegateDAL.Add(dl);
                        hf.Model.CaseDelegate cd = DelegateController.getController().getCaseDelegateModel(DelegateID);
                        cd.ReplyCount++;
                        new hf.DAL.CaseDelegate().Update(cd);
                        result.isTrue = true;
                        result.Message = "接洽完成，您的信息已经发送给案例委托人！";
                    }
                    catch (Exception err)
                    {
                        result.isTrue = false;
                        result.Message = err.Message;
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 添加一对一咨询内容
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        /// <param name="phone"></param>
        /// <param name="lawyerid"></param>
        /// <returns></returns>
        public HFModel.ResultModel doAddLawyerNote(string title, string content, string phone ,string lawyerid)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            BagePages bp = new BagePages();
            hf.Model.User user = bp.getLoginAccount();
            hf.Model.LawyerNote model = new hf.Model.LawyerNote();
            if (user == null)
            {
                result.isTrue = false;
                result.Message = "请先登录";
            }
            else
            {
                model.Title = title;
                model.LawyerID = int.Parse(lawyerid);
                model.UserID = user.ID;
                model.Phone = phone;
                model.Content = content;
                model.isReply = 0;
                model.LawyerReply = "暂无回复 ... ...";
                try {
                    noteDAL.Add(model);
                    result.isTrue = true;
                    result.Message = "咨询发布成功！请等候律师回复！";
                }
                catch (Exception err)
                {
                    result.isTrue = false;
                    result.Message = err.Message;
                }
            }

            return result;
        }

        /// <summary>
        /// 回答一对一问题
        /// </summary>
        /// <param name="id"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public HFModel.ResultModel doModifyLawyerNote(string id, string content)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            BagePages bp = new BagePages();
            hf.Model.Lawyer lawyer = bp.getLoginLawyer();
            if (lawyer == null)
            {
                result.isTrue = false;
                result.Message = "请先登录！";
            }
            else {
                int total = 0;
                hf.Model.LawyerNote ln = LawyerController.getController().getLawyerNoteList(1, 1, "ID=" + id, out total)[0];
                ln.LawyerReply = content;
                ln.isReply = 1;
                try
                {
                    new hf.DAL.LawyerNote().Update(ln);
                    result.isTrue = true;
                    result.Message = "回答完成！";
                }
                catch (Exception err)
                {
                    result.isTrue = false;
                    result.Message = err.Message;
                }
            }
            return result;
        }

        /// <summary>
        /// 根据ID 得到一条律师列
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.Lawyer getLawyerModel(int ID)
        {
            hf.Model.Lawyer model = new hf.Model.Lawyer();
            int total;

            List<hf.Model.Lawyer> list = LawyerDAL.GetLawyerList(1, 1, "ID=" + ID.ToString(), out total);
            BagePages bp = new BagePages();
            if (list.Count > 0)
            {
                model = list[0];
                model.CityName = bp.getCityName(model.CityID.ToString()).Replace("市", "");
                model.ProvinceName = bp.getProvinceName(model.ProvinceID.ToString()).Replace("省", "");
                List<hf.Model.LegalAffairsBiglClass> classList = LegalAffairsController.getController().getLegalAffairsBiglClassList(10 ,model.Areas);
                foreach (hf.Model.LegalAffairsBiglClass lb in classList)
                {
                    if (model.AreasString == string.Empty || model.AreasString == null)
                    {
                        model.AreasString = lb.ClassName;
                    }
                    else {
                        model.AreasString = model.AreasString + "&nbsp;&nbsp;" + lb.ClassName;
                    }
                }
                if (model.AreasString == string.Empty || model.AreasString == null)
                { }
                else
                {
                    model.AreasString = model.AreasString.Trim();
                }
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 得到律师，排序
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="Key"></param>
        /// <returns></returns>
        public List<hf.Model.Lawyer> getLawyerList(int Count, string Key)
        {
            List<hf.Model.Lawyer> list = new List<hf.Model.Lawyer>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = LawyerDAL.GetLawyerList(Count, 1, strSql, Key, "DESC", out total);

            BagePages bp = new BagePages();
            foreach (hf.Model.Lawyer l in list)
            {
                l.CityName = bp.getCityName(l.CityID.ToString()).Replace("市","");
                l.ProvinceName =bp.getProvinceName(l.ProvinceID.ToString()).Replace("省","");
            }

            return list;
        }

        /// <summary>
        /// 得到指定省的律师总数
        /// </summary>
        /// <param name="ProvinceID"></param>
        /// <returns></returns>
        public int getLawyerCount(int ProvinceID)
        {
            List<hf.Model.Lawyer> list = new List<hf.Model.Lawyer>();

            //没有显示记录
            int PageSize = 1;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "ProvinceID=" + ProvinceID.ToString();

            //获得列表
            list = LawyerDAL.GetLawyerList(PageSize, PageNumber, strSql, out total);

            return total;
        }

        /// <summary>
        /// 得到提问列表 前台用
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.LawyerNote> getLawyerNoteList(int PageSize, int PageNumber, string strWhere, out int total)
        {
            List<hf.Model.LawyerNote> list = new List<hf.Model.LawyerNote>();
            //获得列表
            list = noteDAL.GetLawyerNoteList(PageSize, PageNumber, strWhere, out total);
            return list;
        }

        /// <summary>
        /// 得到列表，前台搜索用
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.Lawyer> getLawyerList(int PageSize, int PageNumber, string strWhere, out int total)
        {
            List<hf.Model.Lawyer> list = new List<hf.Model.Lawyer>();
            //获得列表
            list = LawyerDAL.GetLawyerList(PageSize, PageNumber, strWhere, out total);
            BagePages bp = new BagePages();
            foreach (hf.Model.Lawyer l in list)
            {
                l.CityName = bp.getCityName(l.CityID.ToString()).Replace("市", "");
                l.ProvinceName = bp.getProvinceName(l.ProvinceID.ToString()).Replace("省", "");
                List<hf.Model.LegalAffairsBiglClass> classList = LegalAffairsController.getController().getLegalAffairsBiglClassList(10, l.Areas);
                foreach (hf.Model.LegalAffairsBiglClass lb in classList)
                {
                    if (l.AreasString == string.Empty || l.AreasString == null)
                    {
                        l.AreasString = lb.ClassName;
                    }
                    else
                    {
                        l.AreasString = l.AreasString + "&nbsp;&nbsp;" + lb.ClassName;
                    }
                }
                if (l.AreasString == string.Empty || l.AreasString == null)
                { }
                else
                {
                    l.AreasString = l.AreasString.Trim();
                }
            }
            return list;
        }

        /// <summary>
        /// 得到律师列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.Lawyer> getLawyerList(out string PagerStr)
        {
            List<hf.Model.Lawyer> list = new List<hf.Model.Lawyer>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = LawyerDAL.GetLawyerList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 得到首页推荐律师
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="isIndex"></param>
        /// <returns></returns>
        public List<hf.Model.Lawyer> getLawyerList(int Count, int isIndex)
        {
            List<hf.Model.Lawyer> list = new List<hf.Model.Lawyer>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "isIndex = " + isIndex.ToString();

            //获得列表
            list = LawyerDAL.GetLawyerList(Count, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到擅长的领域的推荐律师
        /// </summary>
        /// <param name="Count"></param>
        /// <param name="isIndex"></param>
        /// <param name="ClassID"></param>
        /// <returns></returns>
        public List<hf.Model.Lawyer> getLayerList(int Count, int isIndex, int ClassID)
        {
            List<hf.Model.Lawyer> list = new List<hf.Model.Lawyer>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "isIndex = " + isIndex.ToString() + "and Areas like '%"  + ClassID.ToString() + "%'";

            //获得列表
            list = LawyerDAL.GetLawyerList(Count, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 通过ID数组得到律师列表
        /// </summary>
        /// <param name="LoadCount"></param>
        /// <param name="IDS"></param>
        /// <returns></returns>
        public List<hf.Model.Lawyer> getLawyerList(int LoadCount ,List<hf.Model.Answer> answerList)
        {
            List<hf.Model.Lawyer> list = new List<hf.Model.Lawyer>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";
            string strTempSql = "";
            foreach (hf.Model.Answer an in answerList)
            {
                if (strTempSql == "")
                {
                    strTempSql = an.LawyerID.ToString();
                }
                else {
                    strTempSql = strTempSql + "," + an.LawyerID.ToString();
                }
            }
            if (strTempSql != "")
            {
                strSql = "ID in (" + strTempSql + ")";
            }

            //获得列表
            list = LawyerDAL.GetLawyerList(LoadCount, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到每日之星
        /// </summary>
        /// <returns></returns>
        public hf.Model.Lawyer getDayStarLawyer() {
            List<hf.Model.Lawyer> list = new List<hf.Model.Lawyer>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "isDayStar=1";

            //获得列表
            list = LawyerDAL.GetLawyerList(1, 1, strSql, out total);

            if (list.Count > 0)
            {
                return list[0];
            }
            else {
                return null;
            }
        }

        /// <summary>
        /// 得到律师列表，用作绑定下拉框（方法名和下面的一样，这个是重载）
        /// </summary>
        /// <param name="LoadCount">读取记录数</param>
        /// <returns></returns>
        public List<hf.Model.Lawyer> getLawyerList(int LoadCount)
        {
            List<hf.Model.Lawyer> list = new List<hf.Model.Lawyer>();
            //总数据记录
            int total;

            //筛选条件
            string strSql = "";

            //获得列表
            list = LawyerDAL.GetLawyerList(LoadCount, 1, strSql, out total);

            return list;
        }

        /// <summary>
        /// 验证用户名是否被使用
        /// </summary>
        /// <param name="UID"></param>
        /// <returns></returns>
        public bool GetLawyerByUID(string UID)
        {
            int PageSize = 1;
            int PageNumber = 1;
            int total = 0;

            List<hf.Model.Lawyer> list = new List<hf.Model.Lawyer>();

            hf.DAL.User UserDAL = new hf.DAL.User();
            List<hf.Model.User> list1 = new List<hf.Model.User>();

            list = LawyerDAL.GetLawyerList(PageSize, PageNumber, "UID='" + UID + "'", out total);
            list1 = UserDAL.GetUserList(PageSize, PageNumber, "UID='" + UID + "'", out total);
            bool isUser = false;
            if (list.Count > 0 && list1.Count>0)
            {
                isUser = true;
            }

            return isUser;
        }

        /// <summary>
        /// 注册律师用户
        /// </summary>
        /// <param name="UID"></param>
        /// <param name="PWD"></param>
        /// <param name="PWD1"></param>
        /// <param name="Email"></param>
        /// <param name="lblNum"></param>
        public void doAddLawyer(string UID, string PWD, string PWD1, string Email, string lblNum)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Lawyer model = new hf.Model.Lawyer();

            //验证帐号是否被试用
            if (GetLawyerByUID(UID))
            {
                result.isTrue = false;
                result.goUrl = "history.go(-1);";
                result.historyUrl = "history.go(-1);";
                result.Message = "帐号被使用，请重新选择！";
                this.doResult(result);
                return;
            }
            //验证两次密码输入是否一致
            if (PWD != PWD1)
            {
                result.isTrue = false;
                result.goUrl = "history.go(-1);";
                result.historyUrl = "history.go(-1);";
                result.Message = "两次输入的密码不一致！";
                this.doResult(result);
                return;
            }
            //判断验证码
            if (String.Compare(this.geCheckCookiesNum(), lblNum.Trim(), true) != 0)
            {
                result.isTrue = false;
                result.goUrl = "history.go(-1);";
                result.historyUrl = "history.go(-1);";
                result.Message = "验证码不正确！";
                this.doResult(result);
                return;
            }

            model.UID = UID;
            model.PWD = PWD;
            model.ProvinceID = 1;
            model.CityID = 1;
            model.Sex = 1;
            model.Brithday = DateTime.Parse("1980-1-1");
            model.Email = Email;
            model.Phone = "";
            model.Mobile = "";
            model.Address = "";
            model.Fax = "";
            model.Post = "";
            model.WebSite = "";
            model.RealName = "";
            model.Areas = "";
            model.Language = "";
            model.Content="";
            model.LawyerNo = "";
            model.LawyerFirm = "";
            model.Addtime = DateTime.Now;
            model.HeadPic = "";
            model.Point = 0;
            model.Hits = 0;
            model.isIndex = 0;
            model.isDayStar = 0;

            try
            {
                model.ID = LawyerDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='Lawyer.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 管理管添加律师
        /// </summary>
        /// <param name="UID"></param>
        /// <param name="PWD"></param>
        /// <param name="ProvinceID"></param>
        /// <param name="CityID"></param>
        /// <param name="Sex"></param>
        /// <param name="Brithday"></param>
        /// <param name="Email"></param>
        /// <param name="Phone"></param>
        /// <param name="Mobile"></param>
        /// <param name="Address"></param>
        /// <param name="Fax"></param>
        /// <param name="Post"></param>
        /// <param name="WebSite"></param>
        /// <param name="RealName"></param>
        /// <param name="Areas"></param>
        /// <param name="Language"></param>
        /// <param name="Content"></param>
        /// <param name="LawyerNo"></param>
        /// <param name="LawyerFirm"></param>
        /// <param name="HeadPic"></param>
        /// <param name="Point"></param>
        /// <param name="Hits"></param>
        /// <param name="isIndex"></param>
        public void doAdminAddLawyer(string UID, string PWD, int ProvinceID, int CityID, int Sex, DateTime Brithday, string Email, string Phone, string Mobile, string Address, string Fax, string Post, string WebSite, string RealName, string Areas, string Language, string Content, string LawyerNo, string LawyerFirm, string HeadPic, int Point, int Hits, int isIndex, int isDayStar)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Lawyer model = new hf.Model.Lawyer();

            //验证帐号是否被试用
            if (GetLawyerByUID(UID))
            {
                result.isTrue = false;
                result.goUrl = "history.go(-1);";
                result.historyUrl = "history.go(-1);";
                result.Message = "帐号被使用，请重新选择！";
                this.doResult(result);
                return;
            }

            model.UID = UID;
            model.PWD = PWD;
            model.ProvinceID = ProvinceID;
            model.CityID = ProvinceID;
            model.Sex = Sex;
            model.Brithday = Brithday;
            model.Email = Email;
            model.Phone = Phone;
            model.Mobile = Mobile;
            model.Address = Address;
            model.Fax = Fax;
            model.Post = Post;
            model.WebSite = WebSite;
            model.RealName = RealName;
            model.Areas = Areas;
            model.Language = Language;
            model.Content = Content;
            model.LawyerNo = LawyerNo;
            model.LawyerFirm = LawyerFirm;
            model.Addtime = DateTime.Now;
            model.HeadPic = HeadPic;
            model.Point = Point;
            model.Hits = Hits;
            model.isIndex = isIndex;
            model.isDayStar = isDayStar;

            try
            {
                model.ID = LawyerDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='Lawyer.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch(Exception e)
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
                HttpContext.Current.Response.Write(e.ToString());
            }
            this.doResult(result);
        }

        /// <summary>
        /// 律师注册 For Ajax
        /// </summary>
        /// <param name="UID"></param>
        /// <param name="PWD"></param>
        /// <param name="ProvinceID"></param>
        /// <param name="CityID"></param>
        /// <param name="Sex"></param>
        /// <param name="Brithday"></param>
        /// <param name="Email"></param>
        /// <param name="Phone"></param>
        /// <param name="Mobile"></param>
        /// <param name="Address"></param>
        /// <param name="Fax"></param>
        /// <param name="Post"></param>
        /// <param name="WebSite"></param>
        /// <param name="RealName"></param>
        /// <param name="Areas"></param>
        /// <param name="Language"></param>
        /// <param name="Content"></param>
        /// <param name="LawyerNo"></param>
        /// <param name="LawyerFirm"></param>
        /// <param name="HeadPic"></param>
        /// <param name="Point"></param>
        /// <param name="Hits"></param>
        /// <param name="isIndex"></param>
        /// <param name="isDayStar"></param>
        /// <returns></returns>
        public HFModel.ResultModel doAdminAddLawyer_ForAjax(string UID, string PWD, int ProvinceID, int CityID, int Sex, DateTime Brithday, string Email, string Phone, string Mobile, string Address, string Fax, string Post, string WebSite, string RealName, string Areas, string Language, string Content, string LawyerNo, string LawyerFirm, string HeadPic, int Point, int Hits, int isIndex, int isDayStar)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Lawyer model = new hf.Model.Lawyer();

            //验证帐号是否被使用
            if (GetLawyerByUID(UID))
            {
                result.isTrue = false;
                result.goUrl = "history.go(-1);";
                result.historyUrl = "history.go(-1);";
                result.Message = "帐号被使用，请重新选择！";
            }
            else
            {
                model.UID = UID;
                model.PWD = PWD;
                model.ProvinceID = ProvinceID;
                model.CityID = ProvinceID;
                model.Sex = Sex;
                model.Brithday = Brithday;
                model.Email = Email;
                model.Phone = Phone;
                model.Mobile = Mobile;
                model.Address = Address;
                model.Fax = Fax;
                model.Post = Post;
                model.WebSite = WebSite;
                model.RealName = RealName;
                model.Areas = Areas;
                model.Language = Language;
                model.Content = Content;
                model.LawyerNo = LawyerNo;
                model.LawyerFirm = LawyerFirm;
                model.Addtime = DateTime.Now;
                model.HeadPic = HeadPic;
                model.Point = Point;
                model.Hits = Hits;
                model.isIndex = isIndex;
                model.isDayStar = isDayStar;

                try
                {
                    model.ID = LawyerDAL.Add(model);
                    HttpContext.Current.Session["Lawyer"] = model;
                    result.isTrue = true;
                    result.Message = "添加成功！";
                    result.goUrl = "";
                    result.historyUrl = "history.go(-1)";
                }
                catch (Exception e)
                {
                    result.isTrue = false;
                    result.Message = "系统问题，清联系管理员";
                    result.goUrl = "history.go(-1)";
                    result.historyUrl = "history.go(-1)";
                    HttpContext.Current.Response.Write(e.ToString());
                }
            }
            return result;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="UID"></param>
        /// <param name="PWD"></param>
        public void doLawyerLogin(string UID, string PWD)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            int PageSize = 1;
            int PageNumber = 1;
            int total = 0;

            List<hf.Model.Lawyer> list = new List<hf.Model.Lawyer>();
            list = LawyerDAL.GetLawyerList(PageSize, PageNumber, "UID='" + UID + "' and PWD='" + PWD + "'", out total);
            if (list.Count > 0)
            {
                result.isTrue = true;
                //   result.goUrl = "location='usermanage.aspx'";
                result.historyUrl = "history.go(-1);";
                result.Message = "登录成功！";
                HttpContext.Current.Session["User"] = list[0];
            }
            else
            {
                result.isTrue = false;
                result.goUrl = "history.go(-1);";
                result.historyUrl = "history.go(-1);";
                result.Message = "登录失败，请检查用户名密码是否正确";
            }
        }

        /// <summary>
        /// 修改律师资料
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="ProvinceID"></param>
        /// <param name="CityID"></param>
        /// <param name="Sex"></param>
        /// <param name="Brithday"></param>
        /// <param name="Email"></param>
        /// <param name="Phone"></param>
        /// <param name="Mobile"></param>
        /// <param name="Address"></param>
        /// <param name="Fax"></param>
        /// <param name="Post"></param>
        /// <param name="WebSite"></param>
        /// <param name="RealName"></param>
        /// <param name="Areas"></param>
        /// <param name="Language"></param>
        /// <param name="Content"></param>
        /// <param name="LawyerNo"></param>
        /// <param name="LawyerFirm"></param>
        /// <param name="HeadPic"></param>
        public void doModifyLawyer(int ID, int ProvinceID, int CityID, int Sex, DateTime Brithday, string Email, string Phone, string Mobile, string Address, string Fax, string Post, string WebSite, string RealName, string Areas, string Language, string Content, string LawyerNo, string LawyerFirm, string HeadPic)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Lawyer model = this.getLawyerModel(ID);

            model.ProvinceID = ProvinceID;
            model.CityID = CityID;
            model.Sex = Sex;
            model.Brithday = Brithday;
            model.Email = Email;
            model.Phone = Phone;
            model.Mobile = Mobile;
            model.Address = Address;
            model.Fax = Fax;
            model.Post = Post;
            model.WebSite = WebSite;
            model.RealName = RealName;
            model.Areas = Areas;
            model.Language = Language;
            model.Content = Content;
            model.LawyerNo = LawyerNo;
            model.LawyerFirm = LawyerFirm;
            model.HeadPic = HeadPic;

            try
            {
                //执行修改
                LawyerDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='LawyerManageList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ProvinceID"></param>
        /// <param name="CityID"></param>
        /// <param name="Sex"></param>
        /// <param name="bd_y"></param>
        /// <param name="bd_m"></param>
        /// <param name="bd_d"></param>
        /// <param name="Email"></param>
        /// <param name="Phone"></param>
        /// <param name="Mobile"></param>
        /// <param name="Address"></param>
        /// <param name="Fax"></param>
        /// <param name="Post"></param>
        /// <param name="WebSite"></param>
        /// <param name="RealName"></param>
        /// <param name="Language"></param>
        /// <param name="Content"></param>
        /// <param name="LawyerNo"></param>
        /// <param name="LawyerFirm"></param>
        /// <param name="HeadPic"></param>
        /// <returns></returns>
        public HFModel.ResultModel doModifyLawyer(int ProvinceID, int CityID, int Sex, string bd_y ,string bd_m ,string bd_d, string Email, string Phone, string Mobile, string Address, string Fax, string Post, string WebSite, string RealName , string Language, string Content, string LawyerNo, string LawyerFirm, string HeadPic)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            BagePages bp = new BagePages();
            hf.Model.Lawyer model = bp.getLoginLawyer();
            if (model == null)
            {
                result.isTrue = false;
                result.Message = "请先登录！";
            }
            else
            {
                model.ProvinceID = ProvinceID;
                model.CityID = CityID;
                model.Sex = Sex;
                model.Brithday = DateTime.Parse(bd_y + "-" + bd_m + "-" + bd_d);
                model.Email = Email;
                model.Phone = Phone;
                model.Mobile = Mobile;
                model.Address = Address;
                model.Fax = Fax;
                model.Post = Post;
                model.WebSite = WebSite;
                model.RealName = RealName;
                model.Language = Language;
                model.Content = Content;
                model.LawyerNo = LawyerNo;
                model.LawyerFirm = LawyerFirm;

                if (HeadPic != "")
                {
                    model.HeadPic = HeadPic;
                }

                try
                {
                    //执行修改
                    LawyerDAL.Update(model);

                    result.isTrue = true;
                    result.Message = "修改成功！";
                    //  result.goUrl = "location='User.aspx';";
                    result.historyUrl = "history.go(-1)";
                }
                catch(Exception Err)
                {
                    result.isTrue = false;
                    result.Message = Err.Message;
                    result.goUrl = "history.go(-1)";
                    result.historyUrl = "history.go(-1)";
                }
            }
            return result;
        }

        /// <summary>
        /// 律师自己修改密码
        /// </summary>
        /// <param name="oldpwd"></param>
        /// <param name="newpwd"></param>
        /// <returns></returns>
        public HFModel.ResultModel doModifyLawyerPWD(string oldpwd, string newpwd)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            BagePages bp = new BagePages();
            hf.Model.Lawyer lawyer = bp.getLoginLawyer();
            if (lawyer == null)
            {
                result.isTrue = false;
                result.Message = "先登录";
            }
            else {
                if (lawyer.PWD != oldpwd)
                {
                    result.isTrue = false;
                    result.Message = "当前密码错误";
                }
                else {
                    lawyer.PWD = newpwd;
                    try
                    {
                        LawyerDAL.Update(lawyer);
                        result.isTrue = true;
                        result.Message = "密码修改成功，请牢记！";
                    }
                    catch (Exception err)
                    {
                        result.isTrue = false;
                        result.Message = err.Message;
                    }

                }
            }
            return result;
        }

        /// <summary>
        /// 修改自己的擅长领域
        /// </summary>
        /// <param name="areas"></param>
        /// <returns></returns>
        public HFModel.ResultModel doModifyLawyerAreas(string areas)
        { 
            HFModel.ResultModel result = new HFModel.ResultModel();
            BagePages bp = new BagePages();
            hf.Model.Lawyer lawyer = bp.getLoginLawyer();
            if (lawyer == null)
            {
                result.isTrue = false;
                result.Message = "先登录";
            }
            else {
                lawyer.Areas = areas;
                try {
                    LawyerDAL.Update(lawyer);
                    result.isTrue = true;
                    result.Message = "修改成功！";
                }
                catch (Exception err)
                {
                    result.isTrue = false;
                    result.Message = err.Message;
                }
            }
            return result;
        }

        /// <summary>
        /// 管理员修改律师资料
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="PWD"></param>
        /// <param name="Point"></param>
        /// <param name="Hits"></param>
        /// <param name="isIndex"></param>
        /// <param name="ProvinceID"></param>
        /// <param name="CityID"></param>
        /// <param name="Sex"></param>
        /// <param name="Brithday"></param>
        /// <param name="Email"></param>
        /// <param name="Phone"></param>
        /// <param name="Mobile"></param>
        /// <param name="Address"></param>
        /// <param name="Fax"></param>
        /// <param name="Post"></param>
        /// <param name="WebSite"></param>
        /// <param name="RealName"></param>
        /// <param name="Areas"></param>
        /// <param name="Language"></param>
        /// <param name="Content"></param>
        /// <param name="LawyerNo"></param>
        /// <param name="LawyerFirm"></param>
        /// <param name="HeadPic"></param>
        public void doAdminModifyLawyer(int ID, string UID, string PWD, int Point, int Hits, int isIndex, int ProvinceID, int CityID, int Sex, DateTime Brithday, string Email, string Phone, string Mobile, string Address, string Fax, string Post, string WebSite, string RealName, string Areas, string Language, string Content, string LawyerNo, string LawyerFirm, string HeadPic, int isDayStar)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Lawyer model = this.getLawyerModel(ID);
            LawyerController Ctr = new LawyerController();
            Ctr.doModifyLawyer(ID, ProvinceID, CityID, Sex, Brithday, Email, Phone, Mobile, Address, Fax, Post, WebSite, RealName, Areas, Language, Content, LawyerNo, LawyerFirm, HeadPic);

            model.UID = UID;
            model.PWD = PWD;
            model.Point = Point;
            model.Hits = Hits;
            model.isIndex = isIndex;
            model.isDayStar = isDayStar;

            try
            {
                //执行修改
                LawyerDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='lawyerManageList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="oldPWD"></param>
        /// <param name="PWD"></param>
        /// <param name="PWD1"></param>
        public void doModifyLawyerPWD(int ID, string oldPWD, string PWD, string PWD1)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Lawyer model = this.getLawyerModel(ID);

            if (oldPWD != model.PWD)
            {
                result.isTrue = false;
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
                result.Message = "当前密码错误";
                doResult(result);
            }
            else if (PWD != PWD1)
            {
                result.isTrue = false;
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
                result.Message = "两次输入密码不一致";
                doResult(result);
            }
            else
            {
                model.PWD = PWD;

                LawyerDAL.Update(model);
                result.isTrue = true;
                //     result.goUrl = "location='../main.aspx'";
                result.historyUrl = "history.go(-1);";
                result.Message = "修改成功";
                doResult(result);
            }
        }

        /// <summary>
        /// 律师退出
        /// </summary>
        public void doLogout()
        {
            HttpContext.Current.Session["Lawyer"] = null;
            HttpContext.Current.Session.RemoveAll();
            HttpContext.Current.Response.Redirect("index.aspx");
        }

        /// <summary>
        /// 删除律师
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeLawyer(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                LawyerDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='LawyerList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDS"></param>
        public void doDelegeLawyer(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    LawyerDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='LawyerList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
        /// <summary>
        /// 删除律师
        /// </summary>
        /// <param name="ID"></param>
        public void doManageDelegeLawyer(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                LawyerDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='LawyerManageList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDS"></param>
        public void doManageDelegeLawyer(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    LawyerDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='LawyerManageList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
    }
}
